﻿<ListBox x:Class="BadHabit.Factory.Controls.ArtifactListBox"
    x:Name="listBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:BadHabit_Factory_Converters="clr-namespace:BadHabit.Factory.Converters"   
         
    xmlns:props="clr-namespace:BadHabit.Factory.Windows.Properties"

    Drop="listBox_Drop"
    
    BorderThickness="0">
    <ListBox.Resources>
 
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
        </Style>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="3" VerticalAlignment="Top" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="32"/>
                    <ColumnDefinition Width="120"/>
                </Grid.ColumnDefinitions>

                <Image  Grid.Column="0"  Source="{Binding ArtifactGroupType,Converter={StaticResource ArtifactImageConverter}}" Stretch="Uniform" Width="32" Height="32" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,0,3,0"/>

                <StackPanel Grid.Column="1">
                    <TextBlock Text="{Binding ArtifactName}" TextWrapping="Wrap"/>
                    <TextBlock Text="{Binding ArtifactRevision}" Foreground="Gray"  />
                </StackPanel>

                <Grid.ToolTip>
                    <StackPanel>
                        <TextBlock Text="{Binding ActivityName}" FontWeight="Bold" />
                        <TextBlock Text="{Binding ArtifactPath}" />
                        <TextBlock Text="{Binding ArtifactRevision, StringFormat='{}Revision {0}'}" />
                        <TextBlock Text="{Binding CheckInTime}" />
                    </StackPanel>
                </Grid.ToolTip>

                <Grid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{x:Static props:Resources.Open_artifact}" Click="OpenMenuItemClick"/>
                        <Separator/>
                        <MenuItem Header="{x:Static props:Resources.Remove_tail}" Click="RemoveTailMenuItemClick" >
                            <MenuItem.Icon>
                                <Image Source="/Images/TaskStatus/32x32/TASK_CANCELED.png"  Width="16" Height="16" Stretch="Uniform"/>
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </Grid.ContextMenu>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>   
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />               
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,5,0,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock                                                                                                                                                                             
                                                    Text="{Binding Name}"                      
                                                    TextAlignment="Left"        
                                                    FontWeight="Bold"
                                                    Margin="2,2,2,2"
                                                    Foreground="{StaticResource DarkTextBrush}"/>
                            <TextBlock                                                                                                                                                                             
                                                    Text="{Binding Items.Count, StringFormat='{}({0})'}"                      
                                                    TextAlignment="Left"                                                        
                                                    FontWeight="Bold"
                                                    Margin="2,2,2,2"
                                                    Foreground="{StaticResource DarkTextBrush}"/>
                        </StackPanel>
                        <ItemsPresenter  />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListBox.GroupStyle>    
    <ListBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Value="0">
                    <Setter Property="ListBox.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid
                                    Background="White"
                                    VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                    <TextBlock 
                                        TextWrapping="Wrap"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center">
                                        <TextBlock
                                    TextWrapping="Wrap"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    TextAlignment="Center"
                                    Text="{x:Static props:Resources.No_artifacts_found}"/>    
                                        <LineBreak/>
                                 <TextBlock
                                    TextWrapping="Wrap"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                     Foreground="Gray"
                                    TextAlignment="Center"
                                    Text="{x:Static props:Resources.Drop_here_to_add_a_file}">
                                       <TextBlock.Style>
                                           <Style>
                                               <Style.Triggers>
                                                   <Trigger Property="ListBox.AllowDrop" Value="False">
                                                       <Setter Property="TextBlock.Visibility" Value="Collapsed"/>
                                                   </Trigger>
                                               </Style.Triggers>
                                           </Style>
                                       </TextBlock.Style> 
                                    </TextBlock>
                                        </TextBlock>
                                
                                </Grid>
                                
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>
